package com.liujit.standard.basis.generator.engine.base;

import cn.hutool.core.io.FileUtil;
import com.sun.javafx.PlatformUtil;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @Description ADI项目模板生成 引擎
 * @Author LiuJun
 * @Date 2020/8/13 2:05 下午
 */
public abstract class AbstractTemplateEngine extends BaseTemplateEngine {

    private GroupTemplate groupTemplate;

    public AbstractTemplateEngine() {
        initBeetlEngine();
    }

    protected void initBeetlEngine() {
        Properties properties = new Properties();
        properties.put("RESOURCE.root", "");
        properties.put("DELIMITER_STATEMENT_START", "<%");
        properties.put("DELIMITER_STATEMENT_END", "%>");
        properties.put("HTML_TAG_FLAG", "##");
        Configuration cfg = null;
        try {
            cfg = new Configuration(properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
        groupTemplate = new GroupTemplate(resourceLoader, cfg);
    }

    protected void configTemplate(Template template) {
        template.binding("context", super.contextConfig);
        template.binding("controller", super.controllerConfig);
        template.binding("service", super.serviceConfig);
        template.binding("mapper", super.mapperConfig);
        template.binding("model", super.modelConfig);
        template.binding("query", super.queryConfig);
        template.binding("tableInfo", super.tableInfo);
        template.binding("columnStr", super.columnStr);
    }

    protected void generateFile(String template, String filePath) {
        Template pageTemplate = groupTemplate.getTemplate(template);
        configTemplate(pageTemplate);
        if (PlatformUtil.isWindows()) {
            filePath = filePath.replaceAll("/+|\\\\+", "\\\\");
        } else {
            filePath = filePath.replaceAll("/+|\\\\+", "/");
        }
        File file = FileUtil.file(filePath);
        File parentFile = file.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            pageTemplate.renderTo(fileOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void start() {
        // 配置之间的相互依赖
        super.initConfig();
        // 生成模板
        if (super.contextConfig.getControllerSwitch()) {
            generateController();
        }
        if (super.contextConfig.getServiceSwitch()) {
            generateService();
        }
        if (super.contextConfig.getMapperSwitch()) {
            generateMapper();
        }
        if (super.contextConfig.getXmlSwitch()) {
            generateXml();
        }
        if (super.contextConfig.getModelSwitch()) {
            generateModel();
        }
        if (super.contextConfig.getQuerySwitch()) {
            generateQuery();
        }
    }

    /**
     * 生成Controller
     */
    protected abstract void generateController();

    /**
     * 生成Service
     */
    protected abstract void generateService();

    /**
     * 生成Mapper
     */
    protected abstract void generateMapper();

    /**
     * 生成Xml
     */
    protected abstract void generateXml();

    /**
     * 生成Model
     */
    protected abstract void generateModel();

    /**
     * 生成Query
     */
    protected abstract void generateQuery();
}
